package wangjg.coco.actions;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import wangjg.coco.views.Source2ModelDialog;

/**
 * @author wangjigang1
 *
 */
public class Source2ModelAction implements IObjectActionDelegate {
	private Shell shell;
 	/**
	 * Constructor for Action1.
	 */
	public Source2ModelAction() {
		super();
	}
 
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		this.shell = targetPart.getSite().getShell();
	}
 
	public void run(IAction action) {
		this.openDialog(action);
	}
	
	private void openDialog(IAction action) {
		List<IResource> resourceList = new ArrayList<IResource>();
		Iterator iterator = selection.iterator();
		while(iterator.hasNext()){
			Object element = iterator.next();
			if(element instanceof IFile || element instanceof IFolder){
				resourceList.add((IResource) element);
			}else{
				System.out.println("xxx--"+((IProject)element).getLocation());
				System.out.println("xxx--"+((IProject)element).getFullPath());
			}
		}
		if(resourceList.isEmpty()){
			MessageDialog.openWarning(shell, "!", "没有选中文件或文件夹!");
			return;
		}
		
		Source2ModelDialog dialog = new Source2ModelDialog(new Shell());
		dialog.setResourceList(resourceList);
		dialog.open();
	}
	
	private IStructuredSelection selection;
 
	public void selectionChanged(IAction action, ISelection selection) {
		if(selection instanceof  IStructuredSelection){
			this.selection = (IStructuredSelection) selection;
		}
	} 
 
}